package com.hd.trans.framework.click;

import android.os.SystemClock;
import android.view.View;

/**
 * Author: yang
 * Date: 2018/12/29 14:50
 * Desc: 快速点击工具类
 */
public final class XClickUtil {

    /**
     * 最近一次点击的时间
     */
    private static long mLastClickTime;
    private static long mLastIntervalTime;
    /**
     * 最近一次点击的控件ID
     */
    private static int mLastClickViewId;


    private static int mLastClickingViewId;
    private static long mLastClickingTime;
    private static long mCurrentIntervalTime;

    public static int INTERVAL_MILLIS = 500;

    /**
     * 是否是快速点击
     *
     * @param v              点击的控件
     * @param intervalMillis 时间间期（毫秒）
     * @return true:是，false:不是
     */
    public static boolean isFastDoubleClick(View v, long intervalMillis) {
        int viewId = v.getId();
        long time = SystemClock.elapsedRealtime();
        if (Math.abs(time - mLastClickingTime) < intervalMillis && viewId == mLastClickingViewId) {
            //正处于连续点击状态,过滤本次点击事件
            mLastClickingTime = time;
            mLastClickingViewId = viewId;
            return true;
        }
        mLastClickingTime = time;
        mLastClickingViewId = viewId;
        if (Math.abs(time - mLastClickTime) < intervalMillis && viewId == mLastClickViewId) {
            return true;
        } else {
            mLastClickTime = time;
            mLastClickViewId = viewId;
            return false;
        }
    }

    public static boolean isInterval(long intervalMillis) {
        long time = SystemClock.elapsedRealtime();
        if (Math.abs(time - mCurrentIntervalTime) < intervalMillis) {
            //正处于连续点击状态,过滤本次点击事件
            mCurrentIntervalTime = time;
            return true;
        }
        mCurrentIntervalTime = time;
        if (Math.abs(time - mLastIntervalTime) < intervalMillis) {
            return true;
        } else {
            mLastIntervalTime = time;
            return false;
        }
    }

    /**
     * 最近一次点击Item的时间
     */
    private static long mLastItemClickTime;

    /**
     * 最近一次点击Item
     */
    private static String mLastItemClickViewTag;


    private static String mLastItemClickingViewTag;
    private static long mLastItemClickingTime;

    /**
     * 是否是快速Item点击
     */
    public static boolean isFastDoubleItemClick(View v, int position, long intervalMillis) {
        int viewId = v.getId();
        long time = SystemClock.elapsedRealtime();
        String viewTag = String.format("%1$d_%2$d", viewId, position);
        if (Math.abs(time - mLastItemClickingTime) < intervalMillis && viewTag.equals(mLastItemClickingViewTag)) {
            //正处于连续点击状态,过滤本次点击事件
            mLastItemClickingTime = time;
            mLastItemClickingViewTag = viewTag;
            return true;
        }
        mLastItemClickingTime = time;
        mLastItemClickingViewTag = viewTag;
        if (Math.abs(time - mLastItemClickTime) < intervalMillis && viewTag.equals(mLastItemClickViewTag)) {
            return true;
        } else {
            mLastItemClickTime = time;
            mLastItemClickViewTag = viewTag;
            return false;
        }
    }


    private static long lastClickTime;
    private static final long MAX_TIME = 600;
    public static boolean isValidClick(){
        long time = System.currentTimeMillis();
        if(time-lastClickTime<MAX_TIME){
            return false;
        }
        lastClickTime = time;
        return true;
    }

    //限制函数执行间隔
    private static long lastLimitedTime;
    public static boolean isLimitedExecute (long intervalMillis) {
        long time = System.currentTimeMillis();
        if(time - lastLimitedTime < intervalMillis){
            return false;
        }
        lastLimitedTime = time;
        return true;
    }


}
